﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pg.BioMedics.SDR.Services.Impl;

namespace Pg.BioMedics.SDR.Services
{
    /// <summary>
    /// Business logic services factory class. This class provides operations 
    /// 
    /// </summary>
    public static class ServiceFactory
    {
        #region Static properties

        /// <summary>
        /// Gets ISecurityService implementation instance
        /// </summary>
        public static ISecurityService SecurityService
        {
            get
            {
                return Create<ISecurityService>();
            }
        }

        /// <summary>
        /// Gets Managment service instance 
        /// </summary>
        public static IManagementService ManagementService
        {
            get
            {
                return Create<IManagementService>();
            }
        }

        /// <summary>
        /// Gets IndexingService instance
        /// </summary>
        public static IIndexingService IndexingService
        {
            get
            {
                return Create<IIndexingService>();
            }
        }

        /// <summary>
        /// Gets ObserverService instance
        /// </summary>
        public static IObserverService ObserverService
        {
            get
            {
                return Create<IObserverService>();
            }
        }

        /// <summary>
        /// Gets SarchService instance
        /// </summary>
        public static ISearchService SearchService
        {
            get
            {
                return Create<ISearchService>();
            }
        }

        /// <summary>
        /// Gets CacheManagementService instance
        /// </summary>
        public static ICacheManagementService CacheManagementService
        {
            get
            {
                return Create<ICacheManagementService>();
            }
        }

        #endregion

        #region Static methods

        /// <summary>
        /// Creates instance of service implementation.
        /// </summary>
        /// <typeparam name="T">Service interface. Each service interface must derive from IService</typeparam>
        /// <returns>Service instance</returns>
        public static T Create<T>() where T : IService
        {
            if (typeof(T) == typeof(ISecurityService))
                return (T)(ISecurityService)(new SecurityService());
            else if (typeof(T) == typeof(IManagementService))
                return (T)(IManagementService)(new ManagementService());
            else if (typeof(T) == typeof(IIndexingService))
                return (T)(IIndexingService)(new IndexingService());
            else if (typeof(T) == typeof(IObserverService))
                return (T)(IObserverService)(new ObserverService());
            else if (typeof(T) == typeof(ISearchService))
                return (T)(ISearchService)(new SearchService());
            else if (typeof(T) == typeof(ICacheManagementService))
                return (T)(ICacheManagementService)(new CacheManagementService());
            else
                return default(T);
        }

        #endregion
    }
}
